package com.aeroyun.macross.modular.countryside.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aeroyun.macross.core.base.controller.BaseController;
import com.aeroyun.macross.core.log.LogObjectHolder;
import com.aeroyun.macross.modular.countryside.service.ITownService;
import com.aeroyun.macross.modular.countryside.service.IVillageService;
import com.aeroyun.macross.modular.system.model.Town;
import com.aeroyun.macross.modular.system.model.Village;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 乡镇管理控制器
 *
 * @author fengshuonan
 * @Date 2018-08-10 15:57:54
 */
@Controller
@RequestMapping("/dance-api/town")
public class ApiTownController extends BaseController {

    private String PREFIX = "/countryside/town/";

    @Autowired
    private ITownService townService;
    
    @Autowired
    private IVillageService villageService;

    /**
     * 跳转到乡镇管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "town.html";
    }

    /**
     * 跳转到添加乡镇管理
     */
    @RequestMapping("/town_add")
    public String townAdd() {
        return PREFIX + "town_add.html";
    }

    /**
     * 跳转到修改乡镇管理
     */
    @RequestMapping("/town_update/{townId}")
    public String townUpdate(@PathVariable Integer townId, Model model) {
        Town town = townService.selectById(townId);
        model.addAttribute("item",town);
        LogObjectHolder.me().set(town);
        return PREFIX + "town_edit.html";
    }

    /**
     * 获取乡镇管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	
//    	try {
//			Thread.currentThread().sleep(3000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    	
        return townService.selectList(null);
    }

    /**
     * 新增乡镇管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Town town) {
        townService.insert(town);
        return SUCCESS_TIP;
    }

    /**
     * 删除乡镇管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer townId) {
        townService.deleteById(townId);
        return SUCCESS_TIP;
    }

    /**
     * 修改乡镇管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Town town) {
        townService.updateById(town);
        return SUCCESS_TIP;
    }

    /**
     * 乡镇管理详情
     */
    @RequestMapping(value = "/detail/{townId}")
    @ResponseBody
    public Object detail(@PathVariable("townId") Integer townId) {
    	
    	
    	
    	try {
			Thread.currentThread().sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	
    	Map<String,Object> result = new HashMap<String,Object>();
    	result.put("townInfo", townService.selectById(townId));
    	List<Village> villageList = villageService.selectList(new EntityWrapper<Village>().eq("town_id", townId));
    	result.put("villageList", villageList);
        return result;
    }
    
//    /**villageService*/
//    
//    @RequestMapping(value = "/detail/{townId}")
//    @ResponseBody
//    public Object detail(@PathVariable("townId") Integer townId) {
//        return townService.selectById(townId);
//    }
}
